const { Controller } = require('dx-router-admin');

module.exports = class Family extends Controller {
	constructor(ctx) {
		super(ctx);
	}
	// 列表
	async search({family}) {
        if(!family){
            return this.$dx.error('请输入姓氏')
        }
		// 先取本地
		const info = await this.$dx.service.familyOrigin.where('xing', 'like', family).find();
		if (info) {
			return this.$dx.success('获取成功', info);
		}
		// 到API取
		const apiKey = this.$dx.getConfig('api_key');
		const url = `http://apis.juhe.cn/fapigx/surname/query?key=${apiKey}&xing=${family}`;
		const http = new this.$dx.Curl();
		const res = await http.get(url);
		if (res.error_code === 0 && res.reason === 'success') {
			await this.$dx.service.familyOrigin.insert(res.result);
			return this.$dx.success('获取成功', res.result);
		} else {
			return this.$dx.error(res.msg);
		}
	}
};
